package com.api.controller;

import com.api.mapper.BuildingMapper;
import com.api.mapper.ChamberMapper;
import com.api.mapper.HealthScoreMapper;
import com.api.pojo.Chamber;
import com.api.pojo.HealthScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hs")
public class HealthScoreController {

    @Autowired
    HealthScoreMapper hsm;

    @Autowired
    ChamberMapper cm;

    @Autowired
    BuildingMapper bm;

    @RequestMapping("/update")
    public Map<String, Object> update(HealthScore hs) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        hsm.update(hs);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @RequestMapping("/get")
    public Map<String, Object> get(Integer hid, Integer grade, Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        pageNo = --pageNo * pageSize;
        List<HealthScore> list = grade == 0 ? hsm.get(hid, pageNo, pageSize) : hsm.getNotNull(hid, pageNo, pageSize);
        list.forEach(e -> {
            Chamber chamber = cm.getById(e.getCid());
            chamber.setBuilding(bm.getById(chamber.getBid()));
            e.setChamber(chamber);
        });
        map.put("data", list);
        map.put("total", grade == 0 ? hsm.count(hid) : hsm.countNotNull(hid));
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }
}
